package org.waterbook;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;

public class BookView extends View {
	private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	private Typeface mFace;
	private StringBuffer text;
	private int LEFT = 5;
	private int lineHeight = 24;
	private int textSize = 18;
	private int lastOffset = 0;
	private int current = 0;
	private int offset = 0;
	private int ctWidth;
	private int bufferSize;

	public BookView(Context context) {
		super(context);
		text = new StringBuffer();
		mPaint.setTextSize(textSize);

	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (ctWidth == 0 || bufferSize == 0) {
			ctWidth = this.getWidth();
			bufferSize = (ctWidth / (int) mPaint.measureText(" "))
					* (this.getHeight() / lineHeight);
		}
		draw(canvas, true);
	}

	private void draw(Canvas canvas, Boolean drawable) {
		if (text != null) {
			if (canvas != null)
				canvas.drawColor(Color.BLACK);
			mPaint.setColor(Color.WHITE);
			StringBuffer tmpString = new StringBuffer();
			int x = LEFT;
			int lineCount = 1;
			int textPos = 0;
			boolean inTop = true;
			char[] textCharArray = text.toString().toCharArray();
			for (char c : textCharArray) {
				if (lineCount > this.getHeight() / lineHeight) {
					text.delete(0, textPos - 1);
					lastOffset = offset;
					current += offset;
					offset = textPos - 1;
					textPos = 0;
					break;
				}
				textPos++;
				String textLine = tmpString.toString();
				if ('\r' == c) {
					continue;
				}
				if ('\n' == c) {
					if (inTop) {
						continue;
					}
					if (drawable)
						canvas.drawText(textLine, x, lineCount * lineHeight,
								mPaint);
					lineCount++;
					tmpString.delete(0, tmpString.length());
					continue;
				} else {
					inTop = false;
				}
				if (mPaint.measureText(tmpString.toString() + c) < ctWidth - 5) {
					tmpString.append(c);
					if (textPos == textCharArray.length) {
						if (drawable)
							canvas.drawText(tmpString.toString(), x,
									lineCount * 30, mPaint);
					}
				} else {
					if (drawable)
						canvas.drawText(textLine, x, lineCount * lineHeight,
								mPaint);
					lineCount++;
					tmpString.delete(0, tmpString.length());
					tmpString.append(c);
				}

			}
			if (textPos != 0) {
				text.delete(0, textPos);
				lastOffset = offset;
				current += offset;
				offset = textPos - 1;
			}
		}
		WaterBookActivity.readBookFile(supplyBufferLength());
	}

	public void setText(char[] charArray) {
		int i = charArray.length - 1;
		for (; i >= 0; i--) {
			if (0 != charArray[i]) {
				break;
			}
		}
		if (i > 0) {
			this.text.append(charArray, 0, i + 1);

		}
	}

	public int nextPage() {
		if (0 == text.length()) {
			return -1;
		}
		this.invalidate();
		return 1;
	}

	public void lastPage(int pos) {
		current = 0;
		offset = 0;
		while (current + offset != pos && current + offset < pos) {
			System.out.println(current + offset + ":" + pos);
			draw(null, false);
		}
	}

	public int getCurrnet() {
		return current;
	}

	public int getLastPos() {
		return current - lastOffset;
	}

	public int supplyBufferLength() {
		if (text.length() < bufferSize)
			return bufferSize * 2 - text.length();
		return -1;
	}

	public Paint getPaint() {
		// TODO Auto-generated method stub
		return mPaint;
	}

	public void clean() {
		text.delete(0, text.length());
	}

	public void setLineHeight(int lineHeight) {
		this.lineHeight = lineHeight;
	}

	public int getLineHeight() {
		return lineHeight;
	}

	public int getTextSize() {
		return textSize;
	}

	public void setTextSize(int textSize) {
		this.textSize = textSize;
		mPaint.setTextSize(textSize);
	}

}
